package com.huanying.framework;

import org.apache.log4j.Logger;

import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.exceptions.RequestException;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.LinkTemplate;
import com.gexin.rp.sdk.template.style.Style0;
import com.huanying.goodjob.message.Message;

public class BaseUtil {
	private static Logger logger = Logger.getLogger(BaseUtil.class);
	
	private static String appId = "";
	private static String appKey = "";
	private static String masterSecret = "";
	static String host = "http://sdk.open.api.igexin.com/apiex.htm";

	public static void PushtoSingle(String CID, Message message) throws Exception {
		IGtPush push = new IGtPush(host, appKey, masterSecret);
		LinkTemplate template = linkTemplateDemo(message);
		SingleMessage singlemessage = new SingleMessage();
		
		singlemessage.setOffline(true);
		// 离线有效时间，单位为毫秒，可选
		singlemessage.setOfflineExpireTime(24 * 3600 * 1000);
		singlemessage.setData(template);
		// 可选，1为wifi，0为不限制网络环境。根据手机处于的网络情况，决定是否下发
		singlemessage.setPushNetWorkType(0);
		Target target = new Target();
		target.setAppId(appId);
		target.setClientId(CID);
		IPushResult ret = null;
		try {
			ret = push.pushMessageToSingle(singlemessage, target);
		} catch (RequestException e) {
			logger.error("推送消息失败，重新推送", e);
			ret = push.pushMessageToSingle(singlemessage, target, e.getRequestId());
		}
		if (ret != null) {
			logger.info(ret.getResponse().toString());
		} else {
			logger.info("服务器响应异常");
		}
	}

	public static LinkTemplate linkTemplateDemo(Message massage) {
		LinkTemplate template = new LinkTemplate();
		// 设置APPID与APPKEY
		template.setAppId(appId);
		template.setAppkey(appKey);

		Style0 style = new Style0();
		// 设置通知栏标题与内容
		style.setTitle("请输入通知栏标题");
		style.setText(massage.getMessage());
		// 配置通知栏图标
		style.setLogo("icon.png");
		// 配置通知栏网络图标
		style.setLogoUrl("");
		// 设置通知是否响铃，震动，或者可清除
		style.setRing(true);
		style.setVibrate(true);
		style.setClearable(true);
		template.setStyle(style);

		// 设置打开的网址地址
		template.setUrl("http://www.baidu.com");
		return template;
	}
}
